Audio = {}
Audio.__index = Audio
 
-- Constructor
function Audio.new()
    local instance = {
        musicVolume = 0.6,
        vocalVolume = 0.9,
    }
    
    setmetatable(instance, Audio)
    return instance
end

function Audio:loadAudio()
  MOAIUntzSystem.initialize()
  MOAIUntzSystem.setVolume(0.8)

  
  songMenu = MOAIUntzSound.new()
  songMenu:load("resources/audio/loop.ogg")
  songMenu:setVolume(self.musicVolume)
  
  songLevel1 = MOAIUntzSound.new()
  songLevel1:load("resources/audio/kingslanding.ogg")
  songLevel1:setVolume(self.musicVolume)
  
  songLevel2 = MOAIUntzSound.new()
  songLevel2:load("resources/audio/lannister.ogg")
  songLevel2:setVolume(self.musicVolume)
  
  button_confirm = MOAIUntzSound.new()
  button_confirm:load("resources/audio/button_confirm.ogg")
  button_confirm:setVolume(self.vocalVolume)
  
  button_back = MOAIUntzSound.new()
  button_back:load("resources/audio/button_back.ogg")
  button_back:setVolume(self.vocalVolume)
  
  knight_attackmove1 = MOAIUntzSound.new()
  knight_attackmove1:load("resources/audio/knight_attackmove1.ogg")
  knight_attackmove1:setVolume(self.vocalVolume)
  
  knight_attackmove2 = MOAIUntzSound.new()
  knight_attackmove2:load("resources/audio/knight_attackmove2.ogg")
  knight_attackmove2:setVolume(self.vocalVolume)
  
  knight_ordermove1 = MOAIUntzSound.new()
  knight_ordermove1:load("resources/audio/knight_ordermove1.ogg")
  knight_ordermove1:setVolume(self.vocalVolume)
  
  knight_ordermove2 = MOAIUntzSound.new()
  knight_ordermove2:load("resources/audio/knight_ordermove2.ogg")
  knight_ordermove2:setVolume(self.vocalVolume)
  
  knight_what1 = MOAIUntzSound.new()
  knight_what1:load("resources/audio/knight_what1.ogg")
  knight_what1:setVolume(self.vocalVolume)
  
  knight_what2 = MOAIUntzSound.new()
  knight_what2:load("resources/audio/knight_what2.ogg")
  knight_what2:setVolume(self.vocalVolume)
  
  swordsman_attackmove1 = MOAIUntzSound.new()
  swordsman_attackmove1:load("resources/audio/swordsman_attackmove1.ogg")
  swordsman_attackmove1:setVolume(self.vocalVolume)
  
  swordsman_attackmove2 = MOAIUntzSound.new()
  swordsman_attackmove2:load("resources/audio/swordsman_attackmove2.ogg")
  swordsman_attackmove2:setVolume(self.vocalVolume)
  
  swordsman_ordermove1 = MOAIUntzSound.new()
  swordsman_ordermove1:load("resources/audio/swordsman_ordermove1.ogg")
  swordsman_ordermove1:setVolume(self.vocalVolume)
  
  swordsman_ordermove2 = MOAIUntzSound.new()
  swordsman_ordermove2:load("resources/audio/swordsman_ordermove2.ogg")
  swordsman_ordermove2:setVolume(self.vocalVolume)
  
  swordsman_what1 = MOAIUntzSound.new()
  swordsman_what1:load("resources/audio/swordsman_what1.ogg")
  swordsman_what1:setVolume(self.vocalVolume)
  
  swordsman_what2 = MOAIUntzSound.new()
  swordsman_what2:load("resources/audio/swordsman_what2.ogg")
  swordsman_what2:setVolume(self.vocalVolume)
  
  archer_attackmove1 = MOAIUntzSound.new()
  archer_attackmove1:load("resources/audio/archer_attackmove1.ogg")
  archer_attackmove1:setVolume(self.vocalVolume)
  
  archer_attackmove2 = MOAIUntzSound.new()
  archer_attackmove2:load("resources/audio/archer_attackmove2.ogg")
  archer_attackmove2:setVolume(self.vocalVolume)
  
  archer_ordermove1 = MOAIUntzSound.new()
  archer_ordermove1:load("resources/audio/archer_ordermove1.ogg")
  archer_ordermove1:setVolume(self.vocalVolume)
  
  archer_ordermove2 = MOAIUntzSound.new()
  archer_ordermove2:load("resources/audio/archer_ordermove2.ogg")
  archer_ordermove2:setVolume(self.vocalVolume)
  
  archer_what1 = MOAIUntzSound.new()
  archer_what1:load("resources/audio/archer_what1.ogg")
  archer_what1:setVolume(self.vocalVolume)
  
  archer_what2 = MOAIUntzSound.new()
  archer_what2:load("resources/audio/archer_what2.ogg")
  archer_what2:setVolume(self.vocalVolume)
end

function Audio:playMenuAudio()
  songMenu:setLooping(true)
  songMenu:play()
end

function Audio:playKnightMove1()
  if knight_what1:isPlaying() == false and knight_what2:isPlaying() == false then
    knight_ordermove1:play()
  end
end

function Audio:playKnightMove2()
  if knight_what1:isPlaying() == false and knight_what2:isPlaying() == false then
    knight_ordermove2:play()
  end
end

function Audio:playKnightSelect1()
  knight_what1:play()
end

function Audio:playKnightSelect2()
  knight_what2:play()
end

function Audio:playKnightAttack1()
  if knight_what1:isPlaying() == false and knight_what2:isPlaying() == false and knight_ordermove1:isPlaying() == false and knight_ordermove2:    isPlaying() == false then
    knight_attackmove1:play()
  end
end

function Audio:playKnightAttack2()
  if knight_what1:isPlaying() == false and knight_what2:isPlaying() == false and knight_ordermove1:isPlaying() == false and knight_ordermove2:    isPlaying() == false then
    knight_attackmove2:play()
  end
end

function Audio:playSwordFighterAttack1()
  if swordsman_what1:isPlaying() == false and swordsman_what2:isPlaying() == false and swordsman_ordermove1:isPlaying() == false and swordsman_ordermove2:isPlaying() == false then
    swordsman_attackmove1:play()
  end
end

function Audio:playSwordFighterAttack2()
  if swordsman_what1:isPlaying() == false and swordsman_what2:isPlaying() == false and swordsman_ordermove1:isPlaying() == false and swordsman_ordermove2:isPlaying() == false then
    swordsman_attackmove2:play()
  end
end

function Audio:playSwordFighterMove1()
  if swordsman_what1:isPlaying() == false and swordsman_what2:isPlaying() == false and swordsman_ordermove1:isPlaying() == false and swordsman_ordermove2:isPlaying() == false then
    swordsman_ordermove1:play()
  end
end

function Audio:playSwordFighterMove2()
  if swordsman_what1:isPlaying() == false and swordsman_what2:isPlaying() == false and swordsman_ordermove1:isPlaying() == false and swordsman_ordermove2:isPlaying() == false then
    swordsman_ordermove2:play()
  end
end

function Audio:playSwordFighterSelect1()
  swordsman_what1:play()
end

function Audio:playSwordFighterSelect2()
  swordsman_what2:play()
end

function Audio:playArcherAttack1()
  if archer_what1:isPlaying() == false and archer_what2:isPlaying() == false and archer_ordermove1:isPlaying() == false and archer_ordermove2:isPlaying() == false then
    archer_attackmove1:play()
  end
end

function Audio:playArcherAttack2()
  if archer_what1:isPlaying() == false and archer_what2:isPlaying() == false and archer_ordermove1:isPlaying() == false and archer_ordermove2:isPlaying() == false then
    archer_attackmove2:play()
  end
end

function Audio:playArcherMove1()
  if archer_what1:isPlaying() == false and archer_what2:isPlaying() == false and archer_ordermove1:isPlaying() == false and archer_ordermove2:isPlaying() == false then
    archer_ordermove1:play()
  end
end

function Audio:playArcherMove2()
  if archer_what1:isPlaying() == false and archer_what2:isPlaying() == false and archer_ordermove1:isPlaying() == false and archer_ordermove2:isPlaying() == false then
    archer_ordermove2:play()
  end
end

function Audio:playArcherSelect1()
  archer_what1:play()
end

function Audio:playArcherSelect2()
  archer_what2:play()
end

function Audio:buttonConfirm()
  button_confirm:play()
end

function Audio:buttonBack()
  button_back:play()
end

function Audio:startMenuAudio()
  songMenu:play()
end

function Audio:stopMenuAudio()
  songMenu:stop()
end

function Audio:playLevelBackgroundAudio(song)
  MOAIUntzSystem.setVolume(self.musicVolume)
  local timer = MOAITimer.new()
  timer:setMode(MOAITimer.NORMAL)
  timer:setSpan(0.25)
  timer:setListener(MOAITimer.EVENT_TIMER_END_SPAN, function() self:playWithDelay(song) end)
  timer:start()
end

function Audio:playWithDelay(song)
  if song == 1 then
    songLevel1:setLooping(true)
    songLevel1:play()
  elseif song == 2 then
    songLevel2:setLooping(true)
    songLevel2:play()
  end        
end

function Audio:stopLevelBackgroundAudio()
  songLevel1:stop()
  songLevel2:stop()
  startMenuAudio:setLooping(true)
  startMenuAudio:play()
  
end
 
return Audio